package logic

import (
	"context"
	"douban/doubanrpc/doubanrpc"

	"douban/api/internal/svc"
	"douban/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ProductAddLogic) ProductAdd(req *types.ProductAddRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	add, err := l.svcCtx.DouBanRpc.ProductAdd(l.ctx, &doubanrpc.ProductAddRequest{
		Image: req.Image,
		Name:  req.Name,
		Price: float32(req.Price),
		Stock: req.Stock,
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    500,
			Data:    nil,
		}, nil
	} else {
		return &types.Response{
			Message: "商品添加成功",
			Code:    200,
			Data:    add,
		}, nil
	}

}
